package cn.com.okgotest.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import cn.com.okgo.OkGo;
import cn.com.okgo.cache.CacheMode;
import cn.com.okgo.callback.HttpParsersCallback;
import cn.com.okgo.callback.HttpStringCallback;
import cn.com.okgotest.R;
import cn.com.okgotest.bean.TestBean;
import cn.com.okgotest.fragment.base.BaseFragment;

/**
 * Created by luwenlong on 2017/9/5.
 */

public class ParsersFragment extends BaseFragment {

    private String url = "http://sjzqali.csc108.com:9800/api/tg-service/portfolio/zhlb/select/100000?userID=-1&zhlx=0&page=1&count=5";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_parsers, container, false);
        init(view);
        return view;
    }

    private void init(View view) {

        final TextView httpDataTv=view.findViewById(R.id.httpDataTv);
        final TextView cacheDataTv=view.findViewById(R.id.cacheDataTv);

        final Button button1 = view.findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OkGo.get(url)
                        .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                        .execute(new HttpParsersCallback<List<TestBean>>() {

                            @Override
                            public void onSuccess(List<TestBean> obj, String baseUrl) {
                                httpDataTv.setText(obj.toString());
                            }

                            @Override
                            public void onCache(List<TestBean> obj, String baseUrl) {
                                cacheDataTv.setText(obj.toString());
                            }

                            @Override
                            public void onFailed(Exception e, String baseUrl) {
                                button1.setText("返回错误");
                            }
                        });
            }
        });


    }

}
